<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        var params = [1, 2, 3]

        function testFun(...args) {
            console.log(this);
            console.log(Math.max.apply(null, args));
        }

        let testObj = {
            name: '张三',
            age: 18
        }

        //testFun.apply(testObj, params)



        Function.prototype.myApply = function (context, paramsArr) {
            if (context == null && context == undefined) {
                context = window
            } else {
                context = Object(context)
            }

            //定义函数，判断是参数是否为伪数组
            function isArrayLike(arr) {
                if (arr &&                              //o不是null/undefined
                    (typeof arr === 'object') &&        //o是对象
                    isFinite(arr.length) &&             //o的长度是有限的
                    arr.length >= 0 &&                  //o的长度大于0
                    arr.length === Math.floor(arr.length) &&//o的长度是整数
                    arr.length < 4294967296){             //o的长度小于2^32
                        return true
                    }else{
                        return false
                    }              

            }

            const tempMethod = Symbol('挂载到this目标对象上的待执行函数')
            context[tempMethod] = this
            let result = null;
            //判断参数是否存在
            if (arguments[1]) {
                if (Array.isArray(arguments[1]) && isArrayLike(arguments[1])) {
                    let args = Array.from(arguments[1])
                    result = context[tempMethod](...args)
                } else {
                    new Error('输入的参数得是一个数组')
                }
            } else {
                result = context[tempMethod]()
            }
        }

        testFun.myApply(testObj, params)
    </script>
</body>

</html>